﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using CMS.ORM;//请先添加引用
namespace CMS.ORM.DAL
{
    /// <summary>
    /// 数据访问类News。
    /// </summary>
    public class News
    {
        public News()
        { }
        #region  成员方法
        /// <summary>
        /// 得到最大ID
        /// </summary>
        /// <returns></returns>
        public decimal GetMaxId()
        {
            return DbHelperSQL.GetMaxIDDecimal("id", "News");
        }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(decimal id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from News");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Decimal)};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(CMS.ORM.Model.News model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into News(");
            strSql.Append("id,title,date,department,holder,holderRole,labelAmount,tagAmount,voteAmount,publishAmount,pageAmount,currentNode,currentWorkflow)");
            strSql.Append(" values (");
            strSql.Append("@id,@title,@date,@department,@holder,@holderRole,@labelAmount,@tagAmount,@voteAmount,@publishAmount,@pageAmount,@currentNode,@currentWorkflow)");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Decimal,9),
					new SqlParameter("@title", SqlDbType.NVarChar,50),
					new SqlParameter("@date", SqlDbType.DateTime),
					new SqlParameter("@department", SqlDbType.NVarChar,50),
					new SqlParameter("@holder", SqlDbType.NVarChar,50),
					new SqlParameter("@holderRole", SqlDbType.NVarChar,50),
					new SqlParameter("@labelAmount", SqlDbType.SmallInt,2),
					new SqlParameter("@tagAmount", SqlDbType.SmallInt,2),
					new SqlParameter("@voteAmount", SqlDbType.SmallInt,2),
					new SqlParameter("@publishAmount", SqlDbType.SmallInt,2),
					new SqlParameter("@pageAmount", SqlDbType.SmallInt,2),
					new SqlParameter("@currentNode", SqlDbType.Int,4),
					new SqlParameter("@currentWorkflow", SqlDbType.Int,4)};
            parameters[0].Value = model.id;
            parameters[1].Value = model.title;
            parameters[2].Value = model.date;
            parameters[3].Value = model.department;
            parameters[4].Value = model.holder;
            parameters[5].Value = model.holderRole;
            parameters[6].Value = model.labelAmount;
            parameters[7].Value = model.tagAmount;
            parameters[8].Value = model.voteAmount;
            parameters[9].Value = model.publishAmount;
            parameters[10].Value = model.pageAmount;
            parameters[11].Value = model.currentNode;
            parameters[12].Value = model.currentWorkflow;
            try
            {
                DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CMS.ORM.Model.News model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update News set ");
            strSql.Append("title=@title,");
            strSql.Append("date=@date,");
            strSql.Append("department=@department,");
            strSql.Append("holder=@holder,");
            strSql.Append("holderRole=@holderRole,");
            strSql.Append("labelAmount=@labelAmount,");
            strSql.Append("tagAmount=@tagAmount,");
            strSql.Append("voteAmount=@voteAmount,");
            strSql.Append("publishAmount=@publishAmount,");
            strSql.Append("pageAmount=@pageAmount,");
            strSql.Append("currentNode=@currentNode,");
            strSql.Append("currentWorkflow=@currentWorkflow");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Decimal,9),
					new SqlParameter("@title", SqlDbType.NVarChar,50),
					new SqlParameter("@date", SqlDbType.DateTime),
					new SqlParameter("@department", SqlDbType.NVarChar,50),
					new SqlParameter("@holder", SqlDbType.NVarChar,50),
					new SqlParameter("@holderRole", SqlDbType.NVarChar,50),
					new SqlParameter("@labelAmount", SqlDbType.SmallInt,2),
					new SqlParameter("@tagAmount", SqlDbType.SmallInt,2),
					new SqlParameter("@voteAmount", SqlDbType.SmallInt,2),
					new SqlParameter("@publishAmount", SqlDbType.SmallInt,2),
					new SqlParameter("@pageAmount", SqlDbType.SmallInt,2),
					new SqlParameter("@currentNode", SqlDbType.Int,4),
					new SqlParameter("@currentWorkflow", SqlDbType.Int,4)};
            parameters[0].Value = model.id;
            parameters[1].Value = model.title;
            parameters[2].Value = model.date;
            parameters[3].Value = model.department;
            parameters[4].Value = model.holder;
            parameters[5].Value = model.holderRole;
            parameters[6].Value = model.labelAmount;
            parameters[7].Value = model.tagAmount;
            parameters[8].Value = model.voteAmount;
            parameters[9].Value = model.publishAmount;
            parameters[10].Value = model.pageAmount;
            parameters[11].Value = model.currentNode;
            parameters[12].Value = model.currentWorkflow;
            try
            {
                DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
                return false;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return true;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(decimal id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from News ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Decimal)};
            parameters[0].Value = id;
            try
            {
                DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CMS.ORM.Model.News GetModel(decimal id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,title,date,department,holder,holderRole,labelAmount,tagAmount,voteAmount,publishAmount,pageAmount,currentNode,currentWorkflow from News ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Decimal)};
            parameters[0].Value = id;

            CMS.ORM.Model.News model = new CMS.ORM.Model.News();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = decimal.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                model.title = ds.Tables[0].Rows[0]["title"].ToString();
                if (ds.Tables[0].Rows[0]["date"].ToString() != "")
                {
                    model.date = DateTime.Parse(ds.Tables[0].Rows[0]["date"].ToString());
                }
                model.department = ds.Tables[0].Rows[0]["department"].ToString();
                model.holder = ds.Tables[0].Rows[0]["holder"].ToString();
                model.holderRole = ds.Tables[0].Rows[0]["holderRole"].ToString();
                if (ds.Tables[0].Rows[0]["labelAmount"].ToString() != "")
                {
                    model.labelAmount = int.Parse(ds.Tables[0].Rows[0]["labelAmount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["tagAmount"].ToString() != "")
                {
                    model.tagAmount = int.Parse(ds.Tables[0].Rows[0]["tagAmount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["voteAmount"].ToString() != "")
                {
                    model.voteAmount = int.Parse(ds.Tables[0].Rows[0]["voteAmount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["publishAmount"].ToString() != "")
                {
                    model.publishAmount = int.Parse(ds.Tables[0].Rows[0]["publishAmount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["pageAmount"].ToString() != "")
                {
                    model.pageAmount = int.Parse(ds.Tables[0].Rows[0]["pageAmount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["currentNode"].ToString() != "")
                {
                    model.currentNode = int.Parse(ds.Tables[0].Rows[0]["currentNode"].ToString());
                }
                if (ds.Tables[0].Rows[0]["currentWorkflow"].ToString() != "")
                {
                    model.currentWorkflow = int.Parse(ds.Tables[0].Rows[0]["currentWorkflow"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,title,date,department,holder,holderRole,labelAmount,tagAmount,voteAmount,publishAmount,pageAmount,currentNode,currentWorkflow ");
            strSql.Append(" FROM News ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,title,date,department,holder,holderRole,labelAmount,tagAmount,voteAmount,publishAmount,pageAmount,currentNode,currentWorkflow ");
            strSql.Append(" FROM News ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "News";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法
    }
}


